Imports DAL
Imports BAL
Imports System
Imports MyClasses
Imports System.Configuration

Public Class frmDC

#Region "Variable Declaratoins"

    Dim flagVendor As Byte
    Dim _intDCNO As Integer
    Dim flag As Byte
    Dim TotAmount As Double
    Dim btDelete As Byte     '0:- First Record is going to Delete  1: Second or Next record is going to delete 
    Dim intRowCount As Integer = 20  'Default no of Rows in grid
#End Region

#Region " SomeThing Extra"
    'Status Columns  containts
    '1.  "A"  -Active items(not yet Saved or Freezed)
    '2.  "F"  -Freeze Items(Temporary saved not saved under perticular Bill
    '3.  "U"  -Record is updated on Screen  & till not saved
    '4.  "P"  -Used to Printbill only, not to update in database 
    '5.  "S"  -Record is Saved in databser but not cleared from list(/Screen)
    '6.  "D"  -Deleted  
    'THERE WILL BE ONLY ONE VOUCHER ENTRY WILL BE FREEZED
    '-----------------------MESSAGES------------------------------'
    '  1- Select the Item First
    '10112  ' Enter the  BATCH NO 
    '10113  ' Enter the  EXPIRY DATE
    '10114  ' Enter the  QUANTITY
    '10115  ' Enter the  M.R.P.
    '10116  ' Enter the  BILL RATE
    '10117  ' Enter the  SALE RATE
    '10118  ' Enter the  INVOICE ID 
    '-----------------------MESSAGES------------------------------'
#End Region

#Region "Form Events"

    Private Sub frmDC_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.MdiParent = Nothing
    End Sub

    Private Sub frmDC_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If txtVendorID.Enabled = True Then
                    If isUpdate() Then
                        Select Case BCommon.ShowMsgError(10081, "YNC")
                            Case Windows.Forms.DialogResult.Yes
                                Call SaveData("A")
                            Case Windows.Forms.DialogResult.Cancel
                                e.Cancel = True
                            Case Windows.Forms.DialogResult.No
                        End Select
                    End If
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmDC_FormClosing")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmDC_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        'Rauhl_ITS@20073012 : Add following code display help
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            'Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Delivery Challan")
        End If
    End Sub

        Private Sub frmDC_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Call BCommon.SetFormStandards(Me)
            flag = 1
            Call Grid_Settings()
            Call DisplayData()
            txtDcNo.Focus()
            flagVendor = 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmDC_Load")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
#End Region

#Region "Button Events"

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            Me.Close()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSave_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnSave.KeyDown

        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            btnCancel.Focus()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.WaitCursor
        flag = 1
        Call DisplayData()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnCancel_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnCancel.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            btnSearch.Focus()
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim form As New frmMasterBrowse
            form.strSearch = "Delivery Challan"
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            form.ShowDialog()
            _intDCNO = form.strRecordNo
            Call ClearData()
            If _intDCNO <> 0 Then
                Call ViewFreezeDC()
                btnFreeze.Enabled = False
                btnUnFreeze.Enabled = False
                txtDcNo.Enabled = False
                dtpDate.Enabled = False
                txtVendorID.Enabled = False
                cmDVendor.Enabled = False
                txtRemarks.Enabled = False
                cfgItemDtls.AllowEditing = False
                cfgItemDtls.EditOptions = C1.Win.C1FlexGrid.EditFlags.None
                btnNewItem.Enabled = False
                btnSave.Enabled = False
            Else : Call DisplayData()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ViewFreezeDC()
        Dim dtDItems As DataTable
        Dim intCount As Integer
        ''Dim strDate As New Date     'Divya@12/03/2008 
        TotAmount = 0
        Try
            dtDItems = DPurchaseInvoice.GetDCItems(_intDCNO)
            If dtDItems.Rows.Count > 0 Then
                Call Grid_Settings()
                With cfgItemDtls
                    .Rows.Count = (23 - dtDItems.Rows.Count) + dtDItems.Rows.Count
                    For intCount = 0 To dtDItems.Rows.Count - 1
                        .Item(intCount + 1, 0) = dtDItems.Rows(intCount).Item(0)        'ITEM ID
                        .Item(intCount + 1, 1) = dtDItems.Rows(intCount).Item(1)        '"CODE"
                        .Item(intCount + 1, 2) = dtDItems.Rows(intCount).Item(2)        '"NAME"
                        .Item(intCount + 1, 3) = dtDItems.Rows(intCount).Item(4)        '"PACKING"
                        .Item(intCount + 1, 4) = dtDItems.Rows(intCount).Item(3)        '"COMPANY"
                        .Item(intCount + 1, 5) = dtDItems.Rows(intCount).Item(5)        '"BATCH"
                        .Item(intCount + 1, 6) = dtDItems.Rows(intCount).Item(6)       '"MANUFACTURED"
                        'Divya@12/03/2008 
                        ''strDate = dtDItems.Rows(intCount).Item(6)
                        ''.Item(intCount + 1, 6) = Format(strDate, "MMM/yy")        '"MANUFACTURED"
                        ''.Item(intCount + 1, 6) = Replace(.Item(intCount + 1, 6).ToString(), "-", "/")        '"MANUFACTURED"

                        ''strDate = dtDItems.Rows(intCount).Item(7)
                        ''.Item(intCount + 1, 7) = Format(strDate, "MMM/yy")        '"EXPIRY"
                        ''.Item(intCount + 1, 7) = Replace(.Item(intCount + 1, 7).ToString(), "-", "/")
                        .Item(intCount + 1, 7) = dtDItems.Rows(intCount).Item(7)        '"EXPIRY"
                        '>>End Divya
                        .Item(intCount + 1, 8) = dtDItems.Rows(intCount).Item(8)        '"SCHEMEID"
                        If dtDItems.Rows(intCount).Item(20) <> 0 Then
                            .Item(intCount + 1, 10) = dtDItems.Rows(intCount).Item(20)   '"SCHEME"
                        Else
                            .Item(intCount + 1, 10) = 0
                        End If

                        .Item(intCount + 1, 9) = dtDItems.Rows(intCount).Item(10)      '"QUANTITY"
                        .Item(intCount + 1, 11) = dtDItems.Rows(intCount).Item(9)       '"Per Pack"
                        .Item(intCount + 1, 12) = dtDItems.Rows(intCount).Item(11)      '"M.R.P."
                        .Item(intCount + 1, 13) = dtDItems.Rows(intCount).Item(12)      '"BILL RATE"
                        .Item(intCount + 1, 14) = "0"                                   '"TAX ID"
                        .Item(intCount + 1, 15) = ""                                    '"TAX"
                        .Item(intCount + 1, 16) = dtDItems.Rows(intCount).Item(13)      '"AMOUNT"
                        .Item(intCount + 1, 17) = "0.00"                                '"SALE RATE"
                        .Item(intCount + 1, 18) = "F"                                   '"STATUS"
                        .Item(intCount + 1, 19) = "0"                                   '"VOUCHERNO"
                        .Item(intCount + 1, 20) = 0                                     '"SCHEMEAMOUNT"....Vrushali...1/10/2007
                        If dtDItems.Rows(intCount).Item(12) = 0 Then  'Bill Rate is 0
                            .Item(intCount + 1, 21) = DPurchaseInvoice.Calculate_NetRate(0, .Item(intCount + 1, 11), 0, .Item(intCount + 1, 9) + .Item(intCount + 1, 10))  'QTY + SCHEMEQTY
                        Else   'ELSE PASS THE MRP 
                            .Item(intCount + 1, 21) = DPurchaseInvoice.Calculate_NetRate(0, .Item(intCount + 1, 12), 0, .Item(intCount + 1, 9) + .Item(intCount + 1, 10))   'QTY + SCHEMEQTY
                        End If
                        TotAmount = TotAmount + dtDItems.Rows(intCount).Item(13) - dtDItems.Rows(intCount).Item(21)
                    Next
                End With
                txtVoucherNo.Text = dtDItems.Rows(0).Item(15)
                dtpDate.Value = dtDItems.Rows(0).Item(16)
                txtDcNo.Text = dtDItems.Rows(0).Item(17)
                If dtDItems.Rows(0).Item(18) <> 0 Then
                    txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") + Trim(dtDItems.Rows(0).Item(18))
                    cmDVendor.SelectedValue = dtDItems.Rows(0).Item(18)
                Else
                    cmDVendor.SelectedIndex = 0
                End If
                txtRemarks.Text = dtDItems.Rows(0).Item(19)
                txtGrandTotal.Text = Math.Round(TotAmount, 2)
            Else
                Call Grid_Settings()
                BCommon.ShowMsgError(10082)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewFreezeDC")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnSearch.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            btnClose.Focus()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If ValidateData() = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            Call SaveData("A")
            Call DisplayData()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Private Sub btnNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewItem.Click
    '    Try
    '        Me.Cursor = Cursors.WaitCursor
    '        Dim objNewItem As New frmItemMaster
    '        objNewItem.flgItem = 1
    '        objNewItem.ShowDialog()
    '        Dim dtDItems As DataTable
    '        If objNewItem.intItemNo > 0 Then
    '            If ChkAll.Checked = True Then
    '                dtDItems = DItem.GetItemsPI()
    '                cmDItems.DataSource = dtDItems
    '                cmDItems.ValueMember = "ItemID"
    '                cmDItems.DisplayMember = "Name"
    '                cmDItems.Refresh()
    '            Else
    '                dtDItems = DItem.GetItemsPI()
    '            End If
    '        End If
    '        Me.Cursor = Cursors.Default
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnNewItem_Click")
    '        Me.Cursor = Cursors.Default
    '    End Try
    'End Sub
#End Region

#Region "User Defined Functions"

    Private Sub SaveData(ByVal Status As Char)
        Dim intResult As Integer
        Try
            Dim cPurInv As New CPurchaseInvoice
            Dim objPurItems() As CPurchaseItem
            Call SetData(cPurInv, objPurItems)
            intResult = DPurchaseInvoice.SaveDC("S", cPurInv, objPurItems, Status, "DC")
            If intResult > 0 Then
                flag = 1
                BCommon.ShowMsgError(3)
            Else
                flag = 0
                BCommon.ShowMsgError(4)
                Me.Cursor = Cursors.Default
                Exit Sub
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Function ValidateData() As Byte
        Dim intCount As Integer
        Try
            If txtVoucherNo.Text = "" Then
                BCommon.ShowMsgError(10118)
                Return 0
            End If
            If cmDVendor.SelectedValue <= 0 Then
                BCommon.ShowMsgError(10270)
                cmDVendor.Focus()
                Return 0
            End If

            If IsNothing(cfgItemDtls.Item(1, 0)) OrElse Trim(cfgItemDtls.Item(1, 0)) = String.Empty OrElse cfgItemDtls.Item(1, 0) = 0 Then
                BCommon.ShowMsgError(10287) : Return 0
            End If
            With cfgItemDtls
                For intCount = 1 To .Rows.Count - 1
                    If .Item(intCount, 0) = 0 Then Return 1
                    'BATCH
                    If IsNothing(.Item(intCount, 5)) Or Trim(.Item(intCount, 5)) = String.Empty Then
                        BCommon.ShowMsgError(10112)
                        .Row = intCount
                        .Col = 5
                        Return 0
                    End If
                    'MFD DATE
                    If IsNothing(.Item(intCount, 6)) Then
                        BCommon.ShowMsgError(10271)
                        .Row = intCount
                        .Col = 6
                        Return 0
                    End If
                    'EXPIRY DATE
                    If IsNothing(.Item(intCount, 7)) Then
                        BCommon.ShowMsgError(10272)
                        .Row = intCount
                        .Col = 7
                        Return 0
                    End If
                    'MRP

                    If IsNothing(.Item(intCount, 12)) Or .Item(intCount, 12) = 0 Then
                        BCommon.ShowMsgError(10115)
                        .Row = intCount
                        .Col = 12
                        Return 0
                    End If
                Next
            End With

            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub SetData(ByRef cPurInv As CPurchaseInvoice, ByRef objPurItems As CPurchaseItem())
        Dim intCount As Integer, intResult As Integer
        TotAmount = 0
        Try
            With cPurInv
                If _intDCNO <> 0 Then
                    .InvoiceID = _intDCNO
                Else : .InvoiceID = 0
                End If
                .InvoiceSeqID = Trim(txtVoucherNo.Text)
                .VoucherDate = dtpDate.Value
                .BillDate = dtpDate.Value
                .VendorID = cmDVendor.SelectedValue()
                .DcID = Trim(txtDcNo.Text)

                For intCount = 1 To cfgItemDtls.Rows.Count
                    If CDbl(cfgItemDtls.Cols(16).Item(intCount)) = 0.0 Then Me.Cursor = Cursors.Default : Exit For
                    TotAmount = TotAmount + cfgItemDtls.Cols(16).Item(intCount) - cfgItemDtls.Cols(20).Item(intCount)
                Next
                .Remarks = txtRemarks.Text
            End With

            Dim intItems As Integer
            For intCount = 1 To cfgItemDtls.Rows.Count - 2
                If Not IsNothing(cfgItemDtls.Item(intCount, 0)) Then
                    If Trim(cfgItemDtls.Item(intCount, 0)) <> "" OrElse Trim(cfgItemDtls.Item(intCount, 0)) <> "0" Then
                        intItems = intItems + 1
                    End If
                End If
            Next

            ReDim objPurItems(intItems - 1)
            Dim intManufacurer As Integer
            Dim dtbVItems As New DataTable
            For intCount = 0 To intItems - 1
                objPurItems(intCount) = New CPurchaseItem
                With cfgItemDtls
                    objPurItems(intCount).ItemID = .Item(intCount + 1, 0)
                    objPurItems(intCount).CLRcode = .Item(intCount + 1, 5)
                    objPurItems(intCount).ManufactureDate = CType(.Item(intCount + 1, 6), Date)
                    objPurItems(intCount).ExpiryDate = BCommon.GetDatewithLastDay(CType(.Item(intCount + 1, 7), Date))

                    'ITEMS WHICH R NOT TILL SUPPLIED BY BY THAT VENDOR BUT NOW STARTED SUPPLING NEW ITEM

                    dtbVItems = DItem.GetvendorItems(cmDVendor.SelectedValue, .Item(intCount + 1, 0))
                    If dtbVItems.Rows.Count <= 0 Then
                        intManufacurer = DManufacturer.GetManufacturer(.Item(intCount + 1, 0))
                        intResult = DItem.SaveItemsMV(intManufacurer, cmDVendor.SelectedValue, .Item(intCount + 1, 0))
                        If intResult = 0 Then BCommon.ShowMsgError(4) : Me.Cursor = Cursors.Default : Exit Sub
                    End If

                    'SCHEME RELATED CODE START
                    If IsNothing(.Item(intCount + 1, 8)) OrElse .Item(intCount + 1, 8) = 0 Then    'SCHEME IS NOT SELECTED
                        objPurItems(intCount).SchemeID = 0
                        objPurItems(intCount).SchemeDiscount = 0
                        objPurItems(intCount).SchemeItemQty = IIf(Trim(.Item(intCount + 1, 10)) = "", 0, Val(.Item(intCount + 1, 10)))
                        objPurItems(intCount).StockQuantity = Val(.Item(intCount + 1, 9) + objPurItems(intCount).SchemeItemQty)
                    Else
                        Dim Scheme As CScheme = BScheme.GetScheme(.Item(intCount + 1, 8))
                        objPurItems(intCount).SchemeID = Scheme.SchemeID
                        If Scheme.schemeItemID > 0 Then 'FREE ITEM SCHEME
                            objPurItems(intCount).SchemeDiscount = 0
                            objPurItems(intCount).SchemeItemQty = IIf(IsNothing(.Item(intCount + 1, 10)) OrElse Trim(.Item(intCount + 1, 10)) = String.Empty, 0, .Item(intCount + 1, 10))

                            If Scheme.schemeItemID = objPurItems(intCount).ItemID AndAlso Scheme.ApllyTo = "P" Then
                                objPurItems(intCount).StockQuantity = Val(.Item(intCount + 1, 9)) + objPurItems(intCount).SchemeItemQty
                            Else
                                objPurItems(intCount).StockQuantity = Val(.Item(intCount + 1, 9))
                            End If

                        Else 'PRICE RELATED SCHME
                            objPurItems(intCount).SchemeItemQty = 0
                            objPurItems(intCount).SchemeDiscount = 0
                            objPurItems(intCount).StockQuantity = Val(.Item(intCount + 1, 9))
                        End If
                    End If
                    objPurItems(intCount).Quantity = Val(.Item(intCount + 1, 9))
                    objPurItems(intCount).MRP = .Item(intCount + 1, 12)
                    objPurItems(intCount).BillRate = .Item(intCount + 1, 13)
                    If .Item(intCount + 1, 17) <= 0 Then ' SALE PRICE  
                        objPurItems(intCount).SalePrice1 = .Item(intCount + 1, 12)
                    Else
                        objPurItems(intCount).SalePrice1 = .Item(intCount + 1, 17)
                    End If

                    If .Item(intCount + 1, 13) <= 0 Then 'BILL RATE 
                        objPurItems(intCount).Net_Rate = 0  '.Item(intCount + 1, 12) 'MRP
                        objPurItems(intCount).PurchasePrice = 0 '.Item(intCount + 1, 13)
                    Else
                        objPurItems(intCount).Net_Rate = .Item(intCount + 1, 21) 'NET Rate
                        objPurItems(intCount).PurchasePrice = .Item(intCount + 1, 13)
                    End If
                End With

            Next

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Divya 22/11/2007
    Private Sub SetDatepickerMaxMinDate()
        Dim dtDatePkr As DataTable
        Try
            dtDatePkr = BCommon.GetDatePicker()
            If dtDatePkr.Rows.Count > 0 Then
                dtpDate.MinDate = dtDatePkr.Rows(0).Item("fin_start_dt")
                dtpDate.MaxDate = dtDatePkr.Rows(0).Item("fin_end_dt")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetDatepickerMaxMinDate")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub DisplayData()
        Try
            Call ClearData()
            Call SetDatepickerMaxMinDate()   'Divya 22/11/2007
            cfgItemDtls.Cols(2).Editor = Nothing
            ChkAll.Checked = False
            ChkAll.Enabled = False
            dtpEditor.Visible = False
            lblGrndTot.Visible = False
            lblRs.Visible = False
            lblGrndTot.Text = "Grand Total"
            lblRs.Text = ConfigurationManager.AppSettings("Currency")
            txtGrandTotal.Visible = False
            btnFreeze.Enabled = False
            'btnUnFreeze.Enabled = False
            btnFreeze.Visible = True
            BCommon.SetComboValues(cmDVendor, DVendor.GetVendor(), "VendorID", "VendorName")

            lblDCNo.Visible = True
            txtDcNo.Visible = True
            'lblGrndTot.Visible = True
            'txtGrandTotal.Visible = True
            'cfgItemDtls.Rows.Count = 21

            txtVoucherNo.Text = BCommon.GetSequentialID("DC")
            cmDVendor.Focus()
            flagVendor = 1

            'Divya 30/01/2008 : Added for ReadOnly fields
            BCommon.SetTextReadOnly(txtVoucherNo)
            BCommon.SetTextReadOnly(txtGrandTotal)

            '>>End
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ClearData()
        Try
            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                btnUnFreeze.Enabled = False
                btnNewItem.Enabled = False
                btnSave.Enabled = False
                btnFreeze.Enabled = False
                txtDcNo.Enabled = False
                dtpDate.Enabled = False
                txtVendorID.Enabled = False
                cmDVendor.Enabled = False
                txtRemarks.Enabled = False
            Else
                btnUnFreeze.Enabled = True
                btnNewItem.Enabled = True
                btnSave.Enabled = True
                btnFreeze.Enabled = True
                txtDcNo.Enabled = True
                dtpDate.Enabled = True
                txtVendorID.Enabled = True
                cmDVendor.Enabled = True
                txtRemarks.Enabled = True
            End If
            flagVendor = 0
            txtDcNo.Focus()
            ChkAll.Checked = False
            ChkAll.Enabled = False
            txtVoucherNo.Text = ""
            dtpDate.Value = Now.Date()
            cmDVendor.SelectedIndex = -1
            txtGrandTotal.Text = "0.00"
            txtVendorID.Text = ""
            cmDItems.Visible = False
            txtRemarks.Text = ""
            txtDcNo.Text = ""

            With cfgItemDtls
                .Col = 1
                .AllowEditing = True
                .EditOptions = C1.Win.C1FlexGrid.EditFlags.All
                .Clear(C1.Win.C1FlexGrid.ClearFlags.Content, 1, 0, .Rows.Count - 1, .Cols.Count - 1)
                '.Clear(C1.Win.C1FlexGrid.ClearFlags.Style, 1, 1, .Rows.Count - 1, 1)
                '.Rows.RemoveRange(1, .Rows.Count - 1)
                .Rows.Count = 23
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetItemRow(ByVal ItemStatus As Char)
        'Sets the default values for selected row
        Dim intItemID As Integer
        Dim dtbTable As New DataTable
        Try

            With cfgItemDtls
                Select Case .Col
                    Case 1
                        If cmDVendor.SelectedValue > 0 Then intItemID = DItem.ItemCodeExists(Trim(.Item(.Row, .Col))) Else .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content) : BCommon.ShowMsgError(10274) : intItemID = 0 : cmDVendor.Focus()
                    Case 2
                        If cmDVendor.SelectedValue > 0 Then intItemID = cmDItems.SelectedValue Else .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content) : BCommon.ShowMsgError(10274) : cmDVendor.Focus() : intItemID = 0
                End Select
                If intItemID > 0 Then
                    dtbTable = DItem.GetItemDtls(intItemID, cmDVendor.SelectedValue, ChkAll.CheckState)
                    If dtbTable.Rows.Count <= 0 Then .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content) : btnFreeze.Enabled = False : btnUnFreeze.Enabled = False : Me.Cursor = Cursors.Default : Exit Sub
                Else
                    .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                    'Kiran 
                    If .Row = 0 Then btnFreeze.Enabled = False : btnUnFreeze.Enabled = False
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If

                dtbTable.PrimaryKey = New DataColumn() {dtbTable.Columns(0)} 'Item ID
                'add one more column for company
                Dim dtrItem As DataRow = dtbTable.Rows.Find(intItemID)
                If IsNothing(dtrItem) Then Me.Cursor = Cursors.Default : Exit Sub
                .Item(.Row, 0) = dtrItem(0)                                         'ItemID
                .Item(.Row, 1) = dtrItem(1)                                         'Seq ItemID
                .Item(.Row, 2) = dtrItem(2)                                         'Name
                .Item(.Row, 3) = dtrItem(4)                                         'Pack
                .Item(.Row, 4) = dtrItem(3)                                         'Company
                .Item(.Row, 11) = DItem.GetConversionFactor(dtrItem(0))     'Per Pack
                .Item(.Row, 18) = ItemStatus                                        '"STATUS"
            End With
            Call SetDefaultValues()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItemRow")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetDefaultValues()
        Try
            With cfgItemDtls
                .Item(.Row, 8) = 0          '"Stock ID"
                .Item(.Row, 10) = 0          '"Stock Qty" 
                .Item(.Row, 20) = 0          '"Stock Qty" 
                .Item(.Row, 9) = 1          '"QUANTITY" 
                .Item(.Row, 12) = 0         '"M.R.P."
                .Item(.Row, 13) = 0         'BILL RATE
                .Item(.Row, 15) = 0         ' "TAX"
                .Item(.Row, 16) = 0         '"AMOUNT"
                .Item(.Row, 17) = 0         '"SALE RATE"
                .Item(.Row, 6) = Now.Date.AddDays(-1)          '"MANUFACTURED"
                .Item(.Row, 7) = BCommon.GetDatewithLastDay(Now.Date)          '"EXPIRY"
            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetDefaultValues")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function isValidRow(ByVal intCurrentRow As Integer) As Integer
        Try
            With cfgItemDtls
                ' Is Item Selected 
                If IsNothing(.Item(intCurrentRow, 0)) OrElse Trim(.Item(intCurrentRow, 0)) = "" Then Return ShowColError(1)

                'Is Batch Enterd
                If IsNothing(.Item(intCurrentRow, 5)) OrElse Trim(.Item(intCurrentRow, 5)) = "" Then Return ShowColError(5)

                'Is Manufacturing Date Entered Properly
                If IsNothing(.Item(intCurrentRow, 6)) OrElse Trim(.Item(intCurrentRow, 6)) = "" Then .Col = 6 : Return ShowColError(6)

                'Is Expiry Entered Properly
                If IsNothing(.Item(intCurrentRow, 7)) OrElse Trim(.Item(intCurrentRow, 7)) = "" Then .Col = 7 : Return ShowColError(7)

                'IS Quantity Enterd 
                If IsNothing(.Item(intCurrentRow, 9)) OrElse Trim(.Item(intCurrentRow, 9)) <= 0 Then Return ShowColError(9)
                Return 0
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isValidRow")
            Me.Cursor = Cursors.Default
            Return 0
        End Try
    End Function

    Private Function ShowColError(ByVal intCol As Integer) As Byte
        Try
            Select Case intCol
                Case 0 'Item
                    BCommon.ShowMsgError(10111)
                    intCol = 1
                Case 5 : BCommon.ShowMsgError(10112) '"BATCH"
                Case 6 : BCommon.ShowMsgError(10271) '"MANUFACTURED"
                Case 7 : BCommon.ShowMsgError(10272) 'BCommon.ShowMsgError(10113) '"EXPIRY"
                Case 9 : BCommon.ShowMsgError(10114) '"QUANTITY"
                Case 12 : BCommon.ShowMsgError(10115) '"M.R.P."
                Case 13 : BCommon.ShowMsgError(10116) '"BILL RATE"
                Case 17 : BCommon.ShowMsgError(10117) '"SALE RATE"
                Case Else : intCol = 0
            End Select
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowColError")
            Me.Cursor = Cursors.Default
        Finally
            ShowColError = intCol
        End Try
    End Function
#End Region

#Region "Grid Settings"

    Private Sub Grid_Settings()
        Try
            cfgItemDtls.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
            Call SetGridCaptions()
            Call SetColumnWidths()

            'Common Settings

            With cfgItemDtls
                .SelectionMode = C1.Win.C1FlexGrid.SelectionModeEnum.Row
                .Cols(2).Editor = cmDItems
                ' Hidden Columns
                .Cols(0).Visible = False        'Item ID
                .Cols(8).Visible = False        'Scheme ID 
                .Cols(3).Visible = False        'PACK
                .Cols(14).Visible = False       'Tax ID
                .Cols(18).Visible = False       'STATUS
                .Cols(15).Visible = True        'TAX 'Its temporary hideen when taxex will be taken into considration remove it
                .Cols(19).Visible = False       'VOUCHERNO
                .Cols(20).Visible = False       'SCHEMEAMOUNT....Vrushali...1/10/2007
                .Cols(21).Visible = False        'Net Price ....Vrushali...29/11/2007
                .Cols(13).Visible = False       'Bill Rate
                .Cols(16).Visible = False       'Amount

                'NON EDITABLE COLUMNS
                .Cols(0).AllowEditing = False   'ITEM ID
                .Cols(3).AllowEditing = False   'PACKING
                .Cols(4).AllowEditing = False   'COMPANY
                .Cols(16).AllowEditing = False  'AMOUNT
                .Cols(11).AllowEditing = False
                'DISPLAY FORMAT   

                .Cols(9).DataType = System.Type.GetType("System.Int32")
                .Cols(6).DataType = System.Type.GetType("System.DateTime") 'Chandrakant
                .Cols(7).DataType = System.Type.GetType("System.DateTime")
                ''.Cols(6).DataType = System.Type.GetType("System.String") 'Divya@12/03/2008 
                ''.Cols(7).DataType = System.Type.GetType("System.String") 'Divya@12/03/2008 
                .Cols(15).DataType = System.Type.GetType("System.Text")
                .Cols(12).DataType = System.Type.GetType("System.Decimal")
                .Cols(13).DataType = System.Type.GetType("System.Decimal")
                .Cols(16).DataType = System.Type.GetType("System.Decimal")
                .Cols(17).DataType = System.Type.GetType("System.Decimal")
                .Cols(21).DataType = System.Type.GetType("System.Decimal")

                .Cols(10).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightCenter
                '.Cols(6).Format = ConfigurationManager.AppSettings("DateFormat").ToString()  'Chandrakant
                '.Cols(7).Format = ConfigurationManager.AppSettings("DateFormat").ToString()
                .Cols(6).Format = "MMM/yy"
                .Cols(7).Format = "MMM/yy"


                ''dtpEditor.Format = DateTimePickerFormat.Custom
                ''dtpEditor.CustomFormat = ConfigurationManager.AppSettings("DateFormat").ToString()
                .Cols(6).Editor = dtpEditor
                .Cols(7).Editor = dtpEditor
                .Cols(15).Format = "0.00"  '"TAX"
                .Cols(12).Format = "0.00"  '"M.R.P."
                .Cols(13).Format = "0.00"  '"BILL RATE"
                .Cols(16).Format = "0.00"  '"AMOUNT"
                .Cols(17).Format = "0.00"  '"SALE RATE"
                .Cols(21).Format = "0.00"  '"NET RATE"

                .Cols(17).Visible = False
                .Cols(15).Visible = False
            End With

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Grid_Settings")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetGridCaptions()
        Try
            With cfgItemDtls
                .Cols.Count = 22
                .Rows.Fixed = 1
                'CAPTION
                .Cols(0).Caption = "Item ID"
                .Cols(1).Caption = "Code"
                .Cols(2).Caption = "Item Name"
                .Cols(3).Caption = "Packing"
                .Cols(4).Caption = "Comp"
                .Cols(5).Caption = "Batch No."
                .Cols(6).Caption = "MFG"
                .Cols(7).Caption = "EXP"
                .Cols(8).Caption = "SchemeID"
                .Cols(9).Caption = "Qty" '"Scheme"         'now Quantity
                .Cols(10).Caption = "Scheme"       'now Scheme
                .Cols(11).Caption = "Pack"
                .Cols(12).Caption = "MRP"
                .Cols(13).Caption = "Bill Rate"
                .Cols(14).Caption = "Tax ID"
                .Cols(15).Caption = "Tax"
                .Cols(16).Caption = "Amount"
                .Cols(17).Caption = "Sale Rate"
                .Cols(18).Caption = "Status"
                .Cols(19).Caption = "VoucherNO"
                .Cols(20).Caption = "SchemeAmt"
                .Cols(21).Caption = "Net_Price"
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGridCaptions")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetColumnWidths()

        Dim intGridWidth As Integer = cfgItemDtls.Width - 5
        Try
            With cfgItemDtls()
                .Cols(0).Width = 0    'Hidden Column         '"ITEM ID"
                .Cols(1).Width = intGridWidth / 14           '"ITEM CODE"
                .Cols(2).Width = intGridWidth / 15 * 3       '"ITEM NAME"
                .Cols(3).Width = 0     'Hidden Column        '"PACKING"
                .Cols(4).Width = intGridWidth / 13           '"COMPANY"
                .Cols(5).Width = intGridWidth / 8            '"BATCH"
                .Cols(6).Width = intGridWidth / 13           '"MANUFACTURED DATE"

                .Cols(7).Width = intGridWidth / 13           '"EXPIRY DATE"
                .Cols(8).Width = 0     'Hidden Column        '"SCHEMEID"
                .Cols(10).Width = intGridWidth / 13         '"SCHEME QTY" 
                .Cols(9).Width = intGridWidth / 13          '"ITEM QUANTITY"
                .Cols(11).Width = intGridWidth / 12          'Per Pack
                .Cols(12).Width = intGridWidth / 15          '"M.R.P."
                '.Cols(13).Width = intGridWidth / 14          '"PURCHASE RATE"  'Divya@12/03/2008 
                .Cols(14).Width = 0     'Hidden Column       '"TAX ID"
                .Cols(15).Width = 0          '"TAX AMOUNT/VALUE"
                '.Cols(16).Width = intGridWidth / 12          '"AMOUNT"         'Divya@12/03/2008 
                .Cols(17).Width = intGridWidth / 18          '"SALE RATE"
                .Cols(18).Width = 0     'Hidden Column       '"STATUS"
                .Cols(19).Width = 0     'Hidden Column       '"VOUCHERNO"
                .Cols(20).Width = 0     'Hidden Column       '"SCHEMEAMOUNT"....Vrushali...1/10/2007
                .Cols(21).Width = 0    'Hidden Column        'Net Price ....Vrushali...29/11/2007
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetColumnWidths")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
#End Region

#Region "Grid Events"

    Private Sub cfgItemDtls_AfterDeleteRow(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItemDtls.AfterDeleteRow
        btDelete = 0
        If cfgItemDtls.Rows.Count < intRowCount Then cfgItemDtls.Rows.Count += 10
        CalcTotalAmount()
    End Sub

    Private Sub cfgItemDtls_BeforeDeleteRow(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItemDtls.BeforeDeleteRow
        Try
            If btDelete = 0 Then
                If IsNothing(cfgItemDtls(e.Row, 0)) OrElse Trim(cfgItemDtls(e.Row, 0)) = String.Empty Then e.Cancel = True
                If BCommon.ShowMsgError(10084, "YN") = Windows.Forms.DialogResult.Yes Then
                    btDelete = 1
                Else
                    e.Cancel = True
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_BeforeDeleteRow")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgItemDtls_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItemDtls.AfterEdit
        Try
            With cfgItemDtls
                If .Rows.Count <= .Row Then Me.Cursor = Cursors.Default : Exit Sub
                Select Case .Col
                    Case 0 ' "ITEM ID" 'Hidden Coumn
                    Case 1 ' "ITEM CODE"
                        'Check is Valid Code ID Entered if Yes then set its Details to other coumns
                        If Not IsNothing(Trim(.Item(.Row, .Col))) Then
                            Call SetItemRow("A")
                            If Not IsNothing(.Item(.Row, 1)) Or .Item(.Row, 1) <> "" Then
                                btnFreeze.Enabled = True
                                btnUnFreeze.Enabled = True
                            End If
                        End If
                    Case 2 '         "ITEM NAME"
                        If IsNothing(Trim(.Item(.Row, .Col))) Then Me.Cursor = Cursors.Default : Exit Sub
                        Call SetItemRow("A")
                        If Not IsNothing(.Item(.Row, .Col)) Or .Item(.Row, .Col) <> "" Then
                            btnFreeze.Enabled = True
                            btnUnFreeze.Enabled = True
                        End If

                    Case 5   ' "BATCH"
                        'In Musersetting if salerate is acc. to MRP then in MRP column the value of MRP  _
                        'for perticular Item,Batch,Vendor from tTempPurchase is set else it will b set in Sale rate column  
                        'If same batch is present for the item then it will set MFG EXP Date and MRP from database.
                        Dim dtbBatchDetail As DataTable = DPurchaseInvoice.BatchDetail(.Item(.Row, 0), .Item(.Row, 5))
                        If dtbBatchDetail.Rows.Count > 0 Then
                            '.Item(e.Row, 6).UserData = "E"
                            .Item(e.Row, 6) = dtbBatchDetail.Rows(0).Item("MfgDate")
                            .Cols(6).AllowEditing = False
                            .Item(e.Row, 7) = dtbBatchDetail.Rows(0).Item("ExpDate")
                            .Cols(7).AllowEditing = False
                            .Item(e.Row, 12) = dtbBatchDetail.Rows(0).Item("MRP")
                        Else
                            .Cols(6).AllowEditing = True
                            .Item(e.Row, 6) = Now.Date.AddDays(-1)
                            .Cols(7).AllowEditing = True
                            .Item(e.Row, 7) = BCommon.GetDatewithLastDay(Now.Date)
                            .Item(e.Row, 12) = 0
                        End If
                    Case 6   ' "MANUFACTURE"
                    Case 7   ' "EXPIRY"
                    Case 9   ' "QUANTITY" 
                        '*****

                        If InStr(.Item(.Row, 10), "%") Or InStr(.Item(.Row, 10), "Rs.") Then
                        Else
                            If .Item(.Row, 8) <> 0 Then
                                .Item(.Row, 8) = 0
                                .Item(.Row, 10) = ""
                            End If
                        End If
                        If Not IsNothing(.Item(.Row, 13)) And .Item(.Row, 13) <> 0 And Not IsNothing(.Item(.Row, 9)) Then
                            If Trim(.Item(.Row, 10)) <> "" And Trim(.Item(.Row, 13)) <> "" Then
                                .Item(.Row, 16) = (CDbl(.Item(.Row, 9)) * CDbl(.Item(.Row, 13))) - CDbl(.Item(.Row, 20))
                                .Item(.Row, 17) = CDbl(.Item(.Row, 13)) + DPurchaseInvoice.GetMargin(.Item(.Row, 0)) * CDbl(.Item(.Row, 13))
                                'For Schem          Vrushali....29/09/2007
                                If Not IsNothing(.Item(.Row, 10)) AndAlso Trim(.Item(.Row, 10)) <> "" Then
                                    .Item(.Row, 21) = DPurchaseInvoice.Calculate_NetRate(.Item(.Row, 10), .Item(.Row, 16), 0, .Item(.Row, 9))
                                    .Item(.Row, 20) = 0
                                End If
                                'Parameter (Suggested by Micheal)
                                'Scheme Disc is deducted from Amount in grid   Vrushali...07/11/2007
                                .Item(.Row, 16) = (CDbl(.Item(.Row, 9)) * CDbl(.Item(.Row, 13))) - CDbl(.Item(.Row, 20))
                            End If
                        End If

                    Case 10  '"SCHEME"
                        If Not IsNothing(.Item(.Row, 13)) And .Item(.Row, 13) <> 0 And Not IsNothing(.Item(.Row, 9)) Then
                            If Not IsNothing(.Item(.Row, 15)) And .Item(.Row, 15) <> "" Then
                                'If InStr(.Item(.Row, 15), "VAT") Then
                                '    If InStr(.Item(.Row, 15), ",") Then
                                '        Tax = Mid(.Item(.Row, 15), InStr(.Item(.Row, 15), "=") + 1, InStr(.Item(.Row, 15), ",") - (InStr(.Item(.Row, 15), "=") + 1))
                                '    Else
                                '        Tax = Mid(.Item(.Row, 15), InStr(.Item(.Row, 15), "=") + 1, Len((.Item(.Row, 15))))
                                '    End If
                                'End If
                            End If
                            .Item(.Row, 21) = DPurchaseInvoice.Calculate_NetRate(.Item(.Row, 10), .Item(.Row, 16), 0, .Item(.Row, 9))
                            .Item(.Row, 20) = 0
                            .Item(.Row, 8) = 0
                        End If

                    Case 12  ' "M.R.P."
                        '***
                        If Not IsNothing(.Item(.Row, 12)) And Trim(.Item(.Row, 12)) <> "" Then
                            If .Item(.Row, 13) <> 0 Then
                                .Item(.Row, 17) = CDbl(.Item(.Row, 13)) + DPurchaseInvoice.GetMargin(.Item(.Row, 0)) * CDbl(.Item(.Row, 13))
                            Else : .Item(.Row, 17) = CDbl(.Item(.Row, 12))
                            End If
                        End If
                        '***
                    Case 13  ' "BILL RATE"
                        If Not IsNothing(.Item(.Row, 12)) And Trim(.Item(.Row, 12)) <> "" Then
                            If .Item(.Row, 13) <> 0 Then
                                .Item(.Row, 17) = CDbl(.Item(.Row, 13)) + DPurchaseInvoice.GetMargin(.Item(.Row, 0)) * CDbl(.Item(.Row, 13))
                            Else : .Item(.Row, 17) = CDbl(.Item(.Row, 12))
                            End If
                        End If

                        If Not IsNothing(.Item(.Row, 13)) And Not IsNothing(.Item(.Row, 9)) Then
                            If Trim(.Item(.Row, 9)) <> "" And Trim(.Item(.Row, 13)) <> "" Then
                                .Item(.Row, 16) = (CDbl(.Item(.Row, 9)) * CDbl(.Item(.Row, 13))) - CDbl(.Item(.Row, 20))
                                .Item(.Row, 17) = CDbl(.Item(.Row, 13)) + DPurchaseInvoice.GetMargin(.Item(.Row, 0)) * CDbl(.Item(.Row, 13))
                            End If

                            If Not IsNothing(.Item(.Row, 10)) AndAlso Trim(.Item(.Row, 10)) <> "" Then
                                .Item(.Row, 21) = DPurchaseInvoice.Calculate_NetRate(.Item(.Row, 10), .Item(.Row, 16), 0, .Item(.Row, 9))
                                .Item(.Row, 20) = 0
                            End If
                            .Item(.Row, 16) = (CDbl(.Item(.Row, 9)) * CDbl(.Item(.Row, 13))) - CDbl(.Item(.Row, 20))
                        End If
                    Case 14  ' "TAX ID"        'Hidden
                    Case 15  ' "TAX"        'Vrushali.....29/11/2007
                    Case 16  ' "AMOUNT"
                    Case 17  ' "SALE RATE"
                        If CDbl(.Item(.Row, 17)) = 0 Or Trim(.Item(.Row, 17)) = "" Then
                            .Item(.Row, 17) = CDbl(.Item(.Row, 13)) + DPurchaseInvoice.GetMargin(.Item(.Row, 0)) * CDbl(.Item(.Row, 13))
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_AfterEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgItemDtls_AfterRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles cfgItemDtls.AfterRowColChange
        Try
            If e.NewRange.r1 <= e.OldRange.r1 Then Me.Cursor = Cursors.Default : Exit Sub ' Row is not changed '1
            If Trim(cfgItemDtls.Item(e.OldRange.r1, 0)) = "" OrElse IsNothing(cfgItemDtls.Item(e.OldRange.r1, 0)) Then e.Cancel = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_AfterRowColChange")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgItemDtls_EnterCell(ByVal sender As Object, ByVal e As System.EventArgs) Handles cfgItemDtls.EnterCell
        Try
            With cfgItemDtls
                If .Rows(.Row).AllowEditing = False Then Me.Cursor = Cursors.Default : Exit Sub
                If btnSave.Enabled = True Then
                    If .RowSel = .Rows.Count - 1 Then .Rows.Count = .Rows.Count + 10 'Increments no of rows by 10 if reached to last row 
                End If
                'Code End
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_EnterCell")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#End Region

#Region "On Enter Set Focus to Next Control"

    Private Sub cmDVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmDVendor.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            If sender.selectedindex = -1 Then
                sender.text = ""
            Else
                If ChkAll.Enabled = True Then
                    ChkAll.Focus()
                Else
                    cfgItemDtls.Focus() : cfgItemDtls.Col = 1 : cfgItemDtls.Row = 1
                End If
            End If
        End If
    End Sub

    Private Sub dtpDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpDate.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        'e.SuppressKeyPress = True
        If e.KeyCode = Keys.Enter Then
            If dtpDate.Value > Date.Today Then BCommon.ShowMsgError(10280) : dtpDate.Focus() Else txtVendorID.Focus()
        End If
    End Sub

    Private Sub txtDcNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDcNo.KeyDown
        'If e.Shift Then e.SuppressKeyPress = True : Exit Sub
        If e.KeyCode = Keys.Enter Then dtpDate.Focus()

        If e.Shift = True Then
            If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                e.SuppressKeyPress = True
                Me.Cursor = Cursors.Default
                Exit Sub
            End If
        End If

        If BCommon.ValidateText(e.KeyCode, "ALFNUM", False) = False Then
            e.SuppressKeyPress = True
        End If

        'If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then
        '    e.SuppressKeyPress = True
        'End If
    End Sub

    Private Sub txtRemarks_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRemarks.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnSave.Focus()
        ElseIf BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub txtVoucherNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVoucherNo.KeyDown
        If e.KeyCode = Keys.Enter Then
            dtpDate.Focus()
        ElseIf BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
            e.SuppressKeyPress = True
        End If
    End Sub
#End Region

    Private Sub cfgItemDtls_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cfgItemDtls.TextChanged
        Dim intCount As Integer
        Dim dblAmount As Double
        Try
            With cfgItemDtls
                Select Case .Col
                    Case 15
                        For intCount = 1 To .Rows.Count - 1
                            If .Item(.Row, 15) <> Nothing AndAlso .Item(.Row, 15) <> "" Then
                                dblAmount = dblAmount + .Item(.Row, 15)
                            End If
                        Next
                        txtGrandTotal.Text = Math.Round(dblAmount, 2)
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_TextChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgItemDtls_CellChanged(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItemDtls.CellChanged
        Try
            If cfgItemDtls.Col = 13 Or cfgItemDtls.Col = 9 Then
                Call CalcTotalAmount()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_CellChanged")
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub CalcTotalAmount()
        Dim intCount As Integer
        Try
            TotAmount = 0.0
            For intCount = 1 To cfgItemDtls.Rows.Count - 1
                If IsNothing(cfgItemDtls.Cols(0).Item(intCount)) Then Me.Cursor = Cursors.Default : Exit For
                TotAmount = TotAmount + cfgItemDtls.Item(intCount, 16)
            Next
            txtGrandTotal.Text = Math.Round(TotAmount, 2)

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalcTotalAmount")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmDVendor_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmDVendor.SelectedValueChanged
        If flagVendor = 0 Then Me.Cursor = Cursors.Default : Exit Sub
        Try
            'txtVendorID.Text = ""
            If cmDVendor.SelectedValue > 0 Then
                ChkAll.Enabled = True      '**
                txtVendorID.Text = ""
                txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(cmDVendor.SelectedValue.ToString)
                cfgItemDtls.Cols(2).Editor = cmDItems
            Else : txtVendorID.Text = ""
                ChkAll.Enabled = False
                cfgItemDtls.Cols(2).Editor = Nothing
                Call DisplayData()
            End If
            Call DisplayItems()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmDVendor_SelectedValueChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnFreeze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFreeze.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If ValidateData() = 0 Then
                Me.Cursor = Cursors.Default
                Exit Sub
            Else
                Call SaveData("F")
                If flag = 1 Then Call DisplayData()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnFreeze_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ChkAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAll.CheckedChanged
        Call DisplayItems()
    End Sub

    Private Sub DisplayItems()
        Try
            'Dim dt As DateTime = Now
            Me.Cursor = Cursors.WaitCursor
            Dim dtDItems As DataTable
            If ChkAll.Checked = True And cmDVendor.SelectedIndex <= 0 Then 'If vendor is not selected
                dtDItems = DItem.GetItemsPI()
                dtDItems.PrimaryKey = New DataColumn() {dtDItems.Columns(0)} 'Item ID
            ElseIf ChkAll.Checked = True And cmDVendor.SelectedIndex > 0 Then 'If vendor & Check box is selected
                dtDItems = DItem.GetItemsPI()
            Else
                dtDItems = DItem.GetvendorItems(cmDVendor.SelectedValue) 'DItem.GetItems(StrItems)
            End If
            cmDItems.DataSource = dtDItems
            cmDItems.ValueMember = "ItemID"
            cmDItems.DisplayMember = "Name"
            ' MessageBox.Show((Now - dt).ToString)
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayItems")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtVendorID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVendorID.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtVendorID_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVendorID.LostFocus
        Dim intVendId As Integer
        Dim dsVendorId As New DataSet
        Try
            If Me.txtVendorID.Text <> "" Then
                dsVendorId = DVendor.GetVendorId(txtVendorID.Text.ToUpper())
                If dsVendorId.Tables(0).Rows.Count > 0 Then
                    intVendId = Convert.ToInt32(dsVendorId.Tables(0).Rows(0).Item(0).ToString())
                    cmDVendor.SelectedValue = intVendId
                    cmDVendor.Focus()
                Else
                    cmDVendor.SelectedValue = 0
                    BCommon.ShowMsgError(10219)
                    txtVendorID.Text = ""
                    Me.txtVendorID.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtVendorID_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgItemDtls_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles cfgItemDtls.ValidateEdit
        Dim intValue As Object
        Dim intCount As Integer, intItemID As Integer
        Dim StrBatchNo As String = ""

        Try
            With cfgItemDtls
                Select Case .Col
                    Case 5 'Item entry with same ItemID & CLRcode not allowed  Vrushali..02/12/2007
                        If Not IsNothing(.Item(.Row, 0)) AndAlso .Item(.Row, 0) <> 0 AndAlso sender.editor.text() <> "" Then
                            intItemID = .Item(.Row, 0)
                            StrBatchNo = sender.editor.text() '.Item(.Row, 5)
                        End If
                        If .Row > 0 AndAlso intItemID > 0 Then
                            For intCount = 1 To .Rows.Count - 1
                                '.Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                                If .Row <> intCount Then
                                    If .Item(intCount, 0) = intItemID And .Item(intCount, 5) = StrBatchNo Then
                                        e.Cancel = True
                                        BCommon.ShowMsgError(10268)
                                    End If
                                End If
                            Next
                        End If

                    Case 6      'MFD

                        'e.editor.text
                        If dtpEditor.Value >= Date.Today Then
                            'e.Cancel = True
                            BCommon.ShowMsgError(10119)
                            dtpEditor.Value = Date.Today.AddDays(-1)
                            .Editor.Text = dtpEditor.Value
                        End If

                    Case 7      'Expiry Date

                        If Date.Today > dtpEditor.Value Then
                            'e.Cancel = True
                            BCommon.ShowMsgError(10113)
                            dtpEditor.Value = BCommon.GetDatewithLastDay(Date.Today)
                            .Editor.Text = dtpEditor.Value
                        End If

                    Case 10      'Scheme
                        If sender.editor.text() <> "" Then
                            intValue = sender.editor.text()
                            If IsNumeric(intValue) <> True Then
                                e.Cancel = True
                                BCommon.ShowMsgError(10239)
                            End If
                        End If
                    Case 9      'Quantity
                        If Not IsNothing(.Item(e.Row, 9)) And Trim(.Item(e.Row, 9)) <> "" Then
                            If Trim(cfgItemDtls.Editor.Text) <= "0" Or cfgItemDtls.Editor.Text = "" Then
                                e.Cancel = True
                                BCommon.ShowMsgError(10114)
                            End If
                        End If

                    Case 12     'MRP
                        If Not IsNothing(.Item(e.Row, 12)) And Trim(.Item(e.Row, 12)) <> "" Then
                            If IsNumeric(cfgItemDtls.Editor.Text) Then
                                If cfgItemDtls.Editor.Text < 0 Then
                                    BCommon.ShowMsgError(10311)
                                    e.Cancel = True         'Put cursor on same column
                                End If
                            ElseIf cfgItemDtls.Editor.Text = "" Then
                                .Item(e.Row, 12) = "0.00"
                            Else
                                e.Cancel = True
                                BCommon.ShowMsgError(10239)
                            End If

                        End If
                    Case 13     'Bill Rate
                        If Not IsNothing(.Item(e.Row, 13)) And Trim(.Item(e.Row, 13)) <> "" Then
                            If IsNumeric(cfgItemDtls.Editor.Text) Then
                                If cfgItemDtls.Editor.Text > .Item(e.Row, 12) Then
                                    'cfgItemDtls.Editor.Text
                                    BCommon.ShowMsgError(10284)
                                    e.Cancel = True         'Put cursor on same column
                                ElseIf cfgItemDtls.Editor.Text < 0 Then
                                    BCommon.ShowMsgError(10394)
                                    e.Cancel = True         'Put cursor on same column
                                End If
                            ElseIf cfgItemDtls.Editor.Text = "" Then
                                .Item(e.Row, 13) = "0.00"
                            Else
                                e.Cancel = True
                                BCommon.ShowMsgError(10239)
                            End If
                        End If
                End Select

            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_ValidateEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmDItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmDItems.KeyDown
        'Rahul_ITS@20071114
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            If sender.selectedindex = -1 Then sender.text = ""
        End If
    End Sub

    Private Sub cmDItems_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmDItems.Validating
        If sender.selectedindex = -1 Then sender.text = ""
    End Sub
    Private Sub dtpDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dtpDate.Validating
        'If dtpDate.Value > Date.Today Then MessageBox.Show("Invoice Date must not be future date", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information) : dtpDate.Focus() Else txtVendorID.Focus()
        If dtpDate.Value > Date.Today Then BCommon.ShowMsgError(10280) : dtpDate.Focus() Else txtVendorID.Focus()
    End Sub


    Private Sub cfgItemDtls_BeforeRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles cfgItemDtls.BeforeRowColChange
        Try
            If e.OldRange.r1 <> e.NewRange.r1 Then
                'Checks is previous item details filled completely
                Dim intBlankColumn As Integer
                intBlankColumn = isValidRow(e.OldRange.r1)
                If intBlankColumn > 0 Then
                    cfgItemDtls.Col = intBlankColumn
                    e.Cancel = True
                End If
                'End If
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_BeforeRowColChange")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function isUpdate() As Boolean
        Try
            If Trim(txtVendorID.Text) <> txtVendorID.Tag Or Trim(txtDcNo.Text) <> txtDcNo.Tag _
            Or Trim(txtRemarks.Text) <> txtRemarks.Tag Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub btnUnFreeze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnFreeze.Click
        Dim form As New frmMasterBrowse
        Try
            Me.Cursor = Cursors.WaitCursor
            form.strSearch = "DC Freeze Items"
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            form.ShowDialog()
            _intDCNO = form.strRecordNo
            Call ClearData()
            If _intDCNO <> 0 Then
                Call ViewFreezeDC()
                ChkAll.Enabled = True
                cfgItemDtls.Cols(2).Editor = cmDItems
                Call DisplayItems()
            Else : Call DisplayData()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnUnFreeze_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#Region "Scheme & Margin Calculation"

    'Code Start                 Vrushali ....27/11/2007
    Private Sub cfgItemDtls_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cfgItemDtls.KeyDown
        Try
            If e.Control = True Then
                If e.KeyCode = Keys.S Then      'Display Scheme Selection Form
                    If Not IsNothing(cfgItemDtls.Item(cfgItemDtls.Row, 0)) AndAlso cfgItemDtls.Item(cfgItemDtls.Row, 0) <> 0 Then
                        Call Show_Scheme()
                    End If
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgItemDtls_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub Show_Scheme()
        Dim frm As New frmSchemeSelection
        Try
            With cfgItemDtls
                If .Item(.Row, 0) > 0 Then
                    frm._intItemID = .Item(.Row, 0)
                    frm.strDC = "DC"
                    frm.dtInvoicedate = dtpDate.Value
                    frm.ShowDialog()
                    'StrScheme = frm.Scheme
                    'Dim Scheme As CScheme = BScheme.GetScheme(frm._intSchemeID)
                    If frm._intSchemeID > 0 Then
                        .Item(.Row, 8) = frm._intSchemeID
                        If IsNothing(.Item(.Row, 9)) OrElse Trim(.Item(.Row, 9)) = String.Empty Then
                            .Item(.Row, 9) = 0
                        End If
                        .Item(.Row, 10) = BScheme.GetSchemeQty(frm._intSchemeID, .Item(.Row, 9))
                    Else
                        .Item(.Row, 8) = 0
                        .Item(.Row, 10) = 0
                    End If
                Else
                    BCommon.ShowMsgError(10120)  'MessageBox.Show("Select Item First")
                    .Col = 2
                    cmDItems.SelectedValue = 0
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Show_Scheme")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
#End Region

    Private Sub cfgItemDtls_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItemDtls.BeforeEdit
        With cfgItemDtls
            Select Case .Col
                Case 5, 6, 7, 9, 10, 12, 13, 15, 16
                    If .Item(.Row, 0) = 0 Then e.Cancel = True
                Case 1, 2
                    If .Row > 1 AndAlso (IsNothing(.Item(.Row - 1, 0)) OrElse Trim(.Item(.Row - 1, 0)) = String.Empty) Then e.Cancel = True
            End Select
        End With
    End Sub

    Private Sub cfgItemDtls_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgItemDtls.SetupEditor

        With cfgItemDtls
            Select Case .Col
                Case 5  'BATCH
                    Dim txt As TextBox = DirectCast(cfgItemDtls.Editor, TextBox)
                    txt.MaxLength = 20
                    'CType(ctl, TextBox).MaxLength = 20
                Case 9, 10  'QTY 'SCHEME
                    Dim txt As TextBox = DirectCast(cfgItemDtls.Editor, TextBox)
                    txt.MaxLength = 6

                    'CType(ctl, TextBox).MaxLength = 6
                    'Case 10 'SCHEME
                    '    Dim txt As TextBox = DirectCast(cfgItemDtls.Editor, TextBox)
                    '    txt.MaxLength = 6

                    '    'CType(ctl, TextBox).MaxLength = 6
                Case 12, 13, 17 'MRP 'RATE 'SALE RATE
                    Dim txt As TextBox = DirectCast(cfgItemDtls.Editor, TextBox)
                    txt.MaxLength = 13

                    'CType(ctl, TextBox).MaxLength = 13
                    'Case 13 'RATE
                    '    Dim txt As TextBox = DirectCast(cfgItemDtls.Editor, TextBox)
                    '    txt.MaxLength = 13

                    '    'CType(ctl, TextBox).MaxLength = 13
                    'Case 17 'SALE RATE
                    '    Dim txt As TextBox = DirectCast(cfgItemDtls.Editor, TextBox)
                    '    txt.MaxLength = 13

                    'CType(ctl, TextBox).MaxLength = 13
            End Select
        End With
    End Sub

    Private Sub ChkAll_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ChkAll.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        End If
    End Sub

    
End Class

